﻿
using Asp.Net.Infrastructure;
using B.ERP.Domain;
using B.ERP.Services.OutPut;
using B.MES.SLY.Service;
using B.MES.SLY.Service.Input.Bom;
using B.MES.SLY.Service.Input.Material;
using B.MES.SLY.Service.OutPut;
using Infrastructure;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using Standard;
using System.Threading.Tasks;
using System.Transactions;
using Yitter.IdGenerator;
using GetMaterialOutPut = B.MES.SLY.Service.OutPut.GetMaterialOutPut;

namespace B.MES.SLY.API.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class BomController : ControllerBase
    {
        private readonly IMesService mesService;
        private readonly MesRepository<BomModel> bomRepository;

        public BomController(IMesService mesService, MesRepository<BomModel> bomRepository)
        {
            this.mesService = mesService;
            this.bomRepository = bomRepository;
        }
        private static int serialNumber = 1; // 静态变量，用于记录流水号，初始为1
        /// <summary>
        /// 编码（流水号）
        /// </summary>
        /// <param name="codeType"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpGet]
        public string GetCode()
        {
            var result = new ApiResult<string>();
            //获取表中的数量，在该数量上加1
            var count = mesService.GetBomcount().ResultData.Count;
            var serialNumber = count + 1;
            string serialPart = serialNumber.ToString("D7");
            string code = "JNZG-bom" + DateTime.Now.ToString("yyyyMMddHH") + serialPart;
            serialNumber++;
            return code;

        }
        /// <summary>
        /// 物料列表查询分页
        /// </summary>
        /// <param name="searchMaterialInput"></param>
        /// <returns></returns>
        [HttpGet]
        public ApiPaging<Service.OutPut.GetMaterialOutPut> GetMaterial([FromQuery]Service.Input.Material.SearchMaterialInput searchMaterialInput)
        {
            return new ApiPaging<GetMaterialOutPut>();
        }
        /// <summary>
        /// 物料修改
        /// </summary>
        /// <param name="editMaterialInput"></param>
        /// <returns></returns>
        [HttpPut]
        public ApiResult<int> UpdateMaterial(Service.Input.Material.EditMaterialInput editMaterialInput)
        {
           return mesService.UpdateMaterial(editMaterialInput);
        }
        /// <summary>
        /// 获取物料数量
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ApiResult<List<GetMaterialOutPut>> GetMaterialcount()
        {
          return mesService.GetMaterialcount();
        }
       
        /// <summary>
        /// 获取bom列表
        /// </summary>
        /// <param name="getBomOutPut"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpGet]
        public ApiPaging<GetBomOutPut> GetBom([FromQuery]SearchBomInput input)
        {
           return mesService.GetBom(input);
        }
        /// <summary>
        /// 添加bom
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public ApiResult<int> AddBom(AddBomInput input)
        {
            input.BomCode=GetCode();
           return mesService.AddBom(input);
        }
        /// <summary>
        /// 树形BOM
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ApiResult<List<MESTreeBomDTO>> MESTreeBom()
        {
            
            return mesService.MESTreeBom();
        }

        //[HttpGet]
        //public async Task<ApiResult<List<MESTreeBomDTO>>> GetData()
        //{

        //    try
        //    {
        //        var result=new ApiResult<List<MESTreeBomDTO>>();
        //        var url = "http://localhost:5164";
        //        using HttpClient client = new HttpClient();
        //        client.BaseAddress = new Uri(url);
        //        var response =await client.GetStringAsync("/api/ERPBom/TreeBom");
        //      var s=  JsonConvert.DeserializeObject<ApiResult<List<MESTreeBomDTO>>>(response);
        //        //实例化物料
        //        var material = new AddMaterialInput();
        //        //实例化bom
        //        var bom = new AddBomInput();

                
        //        foreach (var item in s.ResultData)
        //        {
        //            var bomentity = bomRepository.GetEntity(x => x.BomCode == item.Bom.BomCode);
        //            if (bomentity != null)
        //            {
        //                continue;
        //            }
        //            bom.BomCode = item.Bom.BomCode;
        //            bom.BomName = item.Bom.BomName;
        //            bom.EffectiveDate = item.Bom.EffectiveDate;
        //            bom.ExpiryDate = item.Bom.ExpiryDate;
        //            bom.ProductCode = item.Bom.ProductCode;
        //            bom.ProductName = item.Bom.ProductName;
        //            bom.Remark = item.Bom.Remark;
        //            bom.Id = YitIdHelper.NextId().ToString();
        //           // list1.Add(bom);
        //            mesService.AddBom(bom);
        //            foreach (var items in item.Children)
        //            {
                       
        //                material.MaterialCode = items.MaterialCode;
        //                material.MaterialName = items.MaterialName;
        //                material.Spec = items.Spec;
        //                material.Unit = items.Unit;
        //                material.Remark = items.Remark;
        //                material.BomId=bom.Id;
        //                //list.Add(material);
        //                mesService.AddMaterial(material);
        //            }
        //        }
        //        Console.WriteLine(response);
        //        result.Code = s.Code;
        //        result.Message = s.Message;
        //        result.ResultData = s.ResultData;
        //        Console.WriteLine(result.ResultData);
        //        return result;
        //    }
        //    catch (Exception ex)
        //    {

        //        Console.WriteLine("请求失败", ex.Message);
        //        return new ApiResult<List<MESTreeBomDTO>>
        //        {
        //            Code = ApiCode.NotFound,
        //            Message = "请求失败",
        //            ResultData = null
        //        };
        //    }

        //}
        
    }
}

